package jinbao.controller;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jinbao.common.Result;
import jinbao.constant.MessageConstant;
import jinbao.pojo.domain.User;
import jinbao.pojo.dto.PageDTO;
import jinbao.pojo.query.UserQuery;
import jinbao.pojo.request.UserDeleteRequest;
import jinbao.pojo.request.UserUpdateRequest;
import jinbao.pojo.response.UserPageResponse;
import jinbao.pojo.request.UserLoginRequest;
import jinbao.pojo.request.UserRegisterRequest;
import jinbao.pojo.vo.UserListItemVO;
import jinbao.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static jinbao.constant.UserConstant.*;


/**
 * 用户接口
 *
 * @author jinbao
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @GetMapping("/test")
    public Result testUsers() {
        List<User> users = userService.searchUsers("普通员工");
        return new Result(2000, true, MessageConstant.TEST_DATE_SUCCESS, users);
    }

    @GetMapping("/currentUser")
    public Result getCurrentUser(HttpServletRequest request) {
        try {
            Object attribute = request.getSession().getAttribute(USER_LOGIN_STATE);
            User currentUser=(User) attribute;
            if(currentUser==null){
                return new Result(2001,false,MessageConstant.GET_CurrentUser_FALL);
            }else {
                return new Result(2000,true,MessageConstant.GET_CurrentUser_SUCCESS,currentUser);
            }
        }catch (Exception e){
            e.printStackTrace();
            return new Result(3000,false,"出现异常，稍等");
        }
        // 从request中获取用户登录状态
//        Object attribute = request.getSession().getAttribute(USER_LOGIN_STATE);
//         // 获取当前用户
//        User currentUser = (User) attribute;
//        // 如果没有当前用户，返回null
//        if (currentUser == null) {
//            return null;
//        } else {
//        // 如果有当前用户，返回当前用户
//            return currentUser;
//        }

    }

    @PostMapping("/register")
    public Long userRegister(@RequestBody UserRegisterRequest userRegisterRequest) {

//        System.out.println("注册");
        if (userRegisterRequest == null) {
            return null;
        }
        //验证码不正确
        if (!userRegisterRequest.getIdentifyingCode().equals(CHECK_ID)) {
            return null;
        }

        String userAccount = userRegisterRequest.getUserAccount();
        String userPassword = userRegisterRequest.getUserPassword();
        String checkPassword = userRegisterRequest.getCheckPassword();
        if (StringUtils.isAnyBlank(userAccount, userPassword, checkPassword)) {
            return null;
        }

        return userService.userRegister(userAccount, userPassword, checkPassword);

    }

    @PostMapping("/login")
    public User userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {

        if (userLoginRequest == null) {
            return null;
        }

        String userAccount = userLoginRequest.getUserAccount();
        String userPassword = userLoginRequest.getUserPassword();
        if (StringUtils.isAnyBlank(userAccount, userPassword)) {
            return null;
        }
//        System.out.println("登录------------------"+userService.userLogin(userAccount, userPassword, request).toString());
        return userService.userLogin(userAccount, userPassword, request);

    }

    @PostMapping("/deleteUser")
    public boolean deleteUser(@RequestBody UserDeleteRequest userListRequest, HttpServletRequest request) {
        return userService.deleteUser(userListRequest.getKey());
    }

    @PostMapping("/updateUser")
    public boolean updateUser(@RequestBody UserUpdateRequest userUpdateRequest, HttpServletRequest request) {
        return userService.updateUser(userUpdateRequest);
    }

    @GetMapping("/search")
    public UserPageResponse userList(int current, int pageSize, HttpServletRequest request) {
        UserQuery userQuery = new UserQuery();
        //设置User查询参数
        if (request.getParameter("username") != null) {
            String username = request.getParameter("username");
            userQuery.setUsername(username);
        }
        if (request.getParameter("userAccount") != null) {
            String userAccount = request.getParameter("userAccount");
            userQuery.setUserAccount(userAccount);
        }
        if (request.getParameter("gender") != null) {
            Integer gender = Integer.valueOf(request.getParameter("gender"));
            userQuery.setGender(gender);
        }
        if (request.getParameter("userStatus") != null) {
            Integer userStatus = Integer.valueOf(request.getParameter("userStatus"));
            userQuery.setUserStatus(userStatus);
        }
        if (request.getParameter("userAuthority") != null) {
            Integer userAuthority = Integer.valueOf(request.getParameter("userAuthority"));
            userQuery.setUserAuthority(userAuthority);
        }
        if (request.getParameter("phone") != null) {
            String phone = request.getParameter("phone");
            userQuery.setPhone(phone);
        }

        //设置通用参数
        userQuery.setCurrentPage(current);
        userQuery.setPageSize(pageSize);
        PageDTO<UserListItemVO> users = userService.userList(userQuery);
        UserPageResponse ruleListResponse = new UserPageResponse();
        ruleListResponse.setData(users.getList());
        ruleListResponse.setTotal((int) users.getTotal());
        ruleListResponse.setSuccess(true);
        return ruleListResponse;
    }

    ;

}
